package com.example.eldersystem.operatedata.service;


import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.mapper.EmployeeMapper;
import com.example.eldersystem.operatedata.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper (EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    public int getEmployeeNum(){
        return employeeMapper.selectEmployeeNum();
    }

    public DataResponse<String> addEmployee(Employee employee){
        DataResponse<String> dataResponse = new DataResponse<>();
        if (employeeMapper.addEmployee(employee)>0){
            dataResponse.setMsg("添加员工成功");
        }else {
            dataResponse.setCode(51);
            dataResponse.setMsg("添加员工失败");
        }
        return dataResponse;
    }

    public DataResponse<List<Employee>> selectEmployee(int from, int num){
        DataResponse<List<Employee>> dataResponse = new DataResponse<>();
        List<Employee> list;
        int total = employeeMapper.selectEmployeeNum();
        if(total<=0){
            dataResponse.setMsg("查询无结果");
            dataResponse.setCode(10);
        }else{
            dataResponse.setMsg("查询成功");
            list = employeeMapper.selectEmployee(from,num);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<List<Employee>> selectEmployeeByName(String userName){
        DataResponse<List<Employee>> dataResponse = new DataResponse<>();
        List<Employee> list;
        int total = employeeMapper.selectEmployeeNum2(userName);
        if(total<=0){
            dataResponse.setMsg("查询无结果");
            dataResponse.setCode(10);
        }else{
            dataResponse.setMsg("查询成功");
            list = employeeMapper.selectEmployeeByName(userName);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<String> updateEmployee(Employee employee){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = employeeMapper.updateEmployee(employee);
        if(num==0){
            dataResponse.setCode(71);
            dataResponse.setMsg("更新失败，查询不到对应的员工");
        }else{
            dataResponse.setMsg("更新成功");
        }
        return dataResponse;
    }

    public DataResponse<String> updateProfile(int id,String profilePhoto){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = employeeMapper.updateProfile(id,profilePhoto);
        dataResponse.setCode(0);
        if(num==0){
            dataResponse.setCode(71);
            dataResponse.setMsg("更新失败，查询不到对应的员工");
        }else{
            dataResponse.setMsg("更新照片成功");
        }
        return dataResponse;
    }


    public DataResponse<String> deleteEmployee(int id){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = employeeMapper.deleteEmployee(id);
        if(num==0){
            dataResponse.setCode(61);
            dataResponse.setMsg("删除失败");
        }else{
            dataResponse.setMsg("删除成功");
        }
        return dataResponse;
    }
}
